﻿using MediatR;
using ProductManagement.Applcation.Command;
using ProductManagement.Infrstructre.Interface;

namespace ProductManagement.Applcation.CommandHanlers
{
    /// <summary>
    /// 产品分类批量删除
    /// </summary>
    public class ProductTypeDeleteCommandHandlers : IRequestHandler<ProductTypeDeleteCommand, int>
    {
        private readonly InterfaceProductsTypeRepository productRepository;

        public ProductTypeDeleteCommandHandlers(InterfaceProductsTypeRepository productRepository)
        {
            this.productRepository = productRepository;
        }

        public async Task<int> Handle(ProductTypeDeleteCommand request, CancellationToken cancellationToken)
        {
            foreach (var item in request.Ids)
            {
                var list = (await productRepository.GetAll()).First(x => x.ProductTypeId == item);
                if (list!=null)
                {
                    list.IsDel = true;
                    list.DelName = request.DelName;
                    list.DelTime = DateTime.Now;
                    await productRepository.UpdateAsync(list);
                }    
            }
            return 1;
        }
    }
}
